package sng.modules.view.components.city.strategy
{
	import flash.events.TimerEvent;
	
	import mx.controls.Alert;
	
	import sng.data.info.BuildingInfo;
	import sng.data.info.GlobalInfo;
	import sng.data.info.GlobalTimer;
	import sng.modules.model.vo.BuildingVO;
	import sng.modules.model.vo.SeedVO;
	import sng.modules.model.vo.UserBuildingVO;
	import sng.modules.view.components.city.logic.unit.BaseBuilding;
	import sng.modules.view.components.city.state.BuildGatherState;
	import sng.modules.view.components.city.state.BuildNewState;
	import sng.modules.view.components.city.state.BuildNormalState;
	import sng.modules.view.components.city.state.BuildSupplyGoodsState;
	import sng.modules.view.components.city.state.EnumBuildState;
	import sng.modules.view.components.city.state.IBuildState;
	import sng.utils.TimeUtil;
	
	/**
	 * 
	 * @author leonliu
	 * 农田策略类
	 * 
	 */	
	public class FarmingStrategy extends Strategy
	{
		public function FarmingStrategy(building:BaseBuilding, bvo:BuildingVO, userbvo:UserBuildingVO)
		{
			super(building, bvo, userbvo);
			
		}
		private var svo:SeedVO;
		public override function createBuildingStrategy():BuildingIncrement
		{
			//1.计算新建开始的数值变化
			GlobalInfo.mainvo.money=GlobalInfo.mainvo.money-m_bvo.buildingprice;
			
			//2.农田建好后，直接进入等待供货状态
			buildStateManage(EnumBuildState.SUPPLYGOODS_STATE)
			
			return new BuildingIncrement();
		}
		
		/**
		 * 种植完种子后触化此方法 
		 * 
		 */		
		public override function supplyGoodsBuildingStrategy():BuildingIncrement
		{
			svo=BuildingInfo.getCurrentSeedVO();
			if(svo==null) return null;
			
			buildStateManage(EnumBuildState.NORMAL_STATE);
			GlobalInfo.mainvo.money=GlobalInfo.mainvo.money-svo.price;
			//2.收获后农田变成正常成长状态   
			m_building.setBuildState(new BuildNormalState());
			//3.增加倒计时
			GlobalTimer.timeFunctionList.addItem({id:m_bvo.buildingid,time:svo.maturetime, func:gatherTimerHandler,run:true});
			
			//5.将状态保存到数据结构中 （要发到后台）
			m_userbvo.nextState=EnumBuildState.GATHER_STATE;//保存下一个状态
			m_userbvo.countdownTime=TimeUtil.getTimeSecond()+svo.maturetime;
			return new BuildingIncrement();
		}
		
		/**
		 *收获作物后触化此方法 
		 * 
		 */		
		public override function gatherBuildingStrategy():Boolean
		{
			if (super.gatherBuildingStrategy())
			{
				if (svo.harvestgoods>(GlobalInfo.mainvo.goodsuplimit-GlobalInfo.mainvo.goods))
				{
					Alert.show("仓库库存不够！");
					return false;
				}
				//1.计算新建后的数值变化
				GlobalInfo.mainvo.goods+=svo.harvestgoods;//每次收获时获取的金钱数量
				//2.修改状态为等待供货状态
				//m_building.setBuildState(new BuildNewState());
				buildStateManage(EnumBuildState.SUPPLYGOODS_STATE);
				return true;
			}
			else
				return false;
			
		}
		
		/**
		 *植物成熟倒计时完成时触化 
		 * 
		 */		
		private function gatherTimerHandler():void
		{
			buildStateManage(EnumBuildState.GATHER_STATE);
		}
		
		/**
		 *心跳 服务状态的心跳
		 * 
		 */		
		protected override function heartBeat(event:TimerEvent):void
		{
			if(tipFrame!=null)
				tip.gotoAndStop(tipFrame);
			switch(m_building.currentState.getiStateType())
			{
				case EnumBuildState.NORMAL_STATE:
				{
					var time:Number = TimeUtil.getTimeSecond();
					
					tip.timeText.text = TimeUtil.getTimeStringFromSecond(m_userbvo.countdownTime-time);
					if(time-m_userbvo.countdownTime>=m_bvo.countdown)
					{		
						GlobalTimer.removeHeartBeatEventListener(heartBeat);//移除监听事件
					}					
					break;
				}
				default:
				{
					
				}
			}
			
		}
		
		/**
		 * 
		 * @param iState
		 * 农田各状态下UI的管理
		 * 
		 */		
		public override function buildStateManage(iState:uint):void
		{
			if (svo==null)
				svo=BuildingInfo.getCurrentSeedVOBySeedID(m_userbvo.para1 as uint);
				m_building.setBuildState(EnumBuildState.getStateById(iState));
			switch (iState)
			{
				case EnumBuildState.NEW_STATE://创建
					tipFrame="farm_needSeed";
					break;
				case EnumBuildState.NORMAL_STATE://成长
					tipFrame = "farm_normal";
					if(m_building.skin.getMC()!=null)
						m_building.skin.getMC().gotoAndStop("升级1");
					break;
				case EnumBuildState.GATHER_STATE://成熟
					tipFrame = "building_gathing";
					if(m_building.skin.getMC()!=null)
						m_building.skin.getMC().gotoAndStop("升级2");
					break;
				case EnumBuildState.WITHERED_STATE://枯萎
					break;
				case EnumBuildState.SUPPLYGOODS_STATE://等待播种
					tipFrame="farm_needSeed";
					if(m_building.skin.getMC()!=null)
						m_building.skin.getMC().gotoAndStop(m_building.currentDirection);
					break;
			}
		}
		
		
		
	}
}